/*
-----------------------------------------------------------------------------
Filename:    WalkingEnemy.cpp
Author: Michael Sebright
-----------------------------------------------------------------------------
Provides functions for the main character WalkingEnemy
*/

#include "WalkingEnemy.h"
#include <ctime>

//Constructor that places Enemy at the specified vector
WalkingEnemy::WalkingEnemy(Camera* cam,Vector3 vect) {
	// Create the entity
	mSceneMgr=cam->getSceneManager();
	this->cam=cam;
    mEntity = mSceneMgr->createEntity("robot.mesh");
    // Create the scene node
    mNode = mSceneMgr->getRootSceneNode()->createChildSceneNode(vect);
	lNode=mNode->createChildSceneNode();
    lNode->attachObject(mEntity);
	lNode->scale( .2, .2, .2 );
	lNode->rotate(Vector3::UNIT_Y,Degree(270));
	    
		mWalkSpeed = 20.0f;
        mDirection = Ogre::Vector3::ZERO;

		mAnimationState = mEntity->getAnimationState("Idle");//Default robot in idle
   mAnimationState->setLoop(true);
   mAnimationState->setEnabled(true);
   
   setupWeapons(mSceneMgr);   
}

//Constructor that places the enemy off to the left of Dredd
WalkingEnemy::WalkingEnemy(Camera* cam) {
	// Create the entity
	mSceneMgr=cam->getSceneManager();
	this->cam=cam;
    mEntity = mSceneMgr->createEntity("robot.mesh");
    // Create the scene node
	Vector3 vect(-100.0,0.0,0.0);
       mNode = mSceneMgr->getRootSceneNode()->createChildSceneNode(vect);
	   lNode=mNode->createChildSceneNode();
    lNode->attachObject(mEntity);
	lNode->scale( .2, .2, .2 );
	lNode->rotate(Vector3::UNIT_Y,Degree(270));
	
      
	
    
		mWalkSpeed = 15.0f;
        mDirection = Ogre::Vector3::ZERO;

	mAnimationState = mEntity->getAnimationState("Idle");//Default robot in idle
   mAnimationState->setLoop(true);
   mAnimationState->setEnabled(true);
   setupWeapons(mSceneMgr);  
}

//Called every frame to update the robot and his bullet
void WalkingEnemy::animate(FrameEvent evt) {
	if(alive){		
			Ogre::Quaternion quat;
             Ogre::Real move = mWalkSpeed * evt.timeSinceLastFrame;
             mDistance -= move;//update distance
			 
                
                            
                    Ogre::Vector3 src = mNode->getOrientation() * Ogre::Vector3::UNIT_Z;
					src.y=0;
					mDirection.y=0;
					if ((1.0f + src.dotProduct(mDirection)) < 0.0001f) //if 180 degree turn
						{
						 mNode->yaw(Ogre::Degree(180));
					 }
					else
						{
						 Ogre::Quaternion quat = src.getRotationTo(mDirection);//rotate toward main character
						 
						mNode->rotate(quat);
						} 
					if(mDistance>125){//if distance is >125 robot stays idle
						mAnimationState = mEntity->getAnimationState("Idle");
						mAnimationState->setLoop(true);
						mAnimationState->setEnabled(true);						
					}else if(mDistance<50){//if distance is <50 run shoot animation
						mAnimationState = mEntity->getAnimationState("Shoot");
						mAnimationState->setLoop(true);
						mAnimationState->setEnabled(true);
						if(ammoIndex==0){
						
							Vector3 vect=getLocation();
							vect.y+=15;
							b.fire(vect,mNode->getOrientation());//fire the bullet
							//b2.fire(vect,mNode->getOrientation());
						}						
						
						ammoIndex++;
						/*if (ammoIndex+1<=NUM_ACTIVE_BULLETS){
							ammoIndex=0;
						}else{
							ammoIndex++;
						}*/

					}else{//Else walk towards main character
						mAnimationState = mEntity->getAnimationState("Walk");
						mAnimationState->setLoop(true);
						mAnimationState->setEnabled(true);
						mNode->translate(mDirection * move);
					}
					
         
	mAnimationState->addTime(evt.timeSinceLastFrame);//update frame
	}else if(deathCounter>90){//if dead for >90 frames set invisiblr
		mEntity->setVisible(false);
		mAnimationState->addTime(evt.timeSinceLastFrame);
	}else if(deathCounter<=90){//If dead for <90 frames run death animation
		mAnimationState->addTime(evt.timeSinceLastFrame);
		deathCounter++;
	}
		if(b.isProjectileActive()){//Update the animation for the bullet
			
			updateBullets(evt.timeSinceLastFrame);
			
		}
}


//Changes the enemies destination to vect
void WalkingEnemy::updateDest(Vector3 vect){
	mDestination = vect;
	mDirection = mDestination - mNode->getPosition();
    mDistance = mDirection.normalise();
}

//Starts the death animation
void WalkingEnemy::setAnimationDie(){
	mAnimationState =  mEntity->getAnimationState("Die");
    mAnimationState->setLoop(false);
    mAnimationState->setEnabled(true);
	alive=false;
	deathCounter=0;
}

//Initializes the bullet
void WalkingEnemy::setupWeapons(SceneManager* sceneMgr) {
	b=Bullet(sceneMgr);
	//b2=Bullet(sceneMgr);
	ammoIndex = 0;
}

//is enemy alive
bool WalkingEnemy::isDead(){
	return !alive;
}


void WalkingEnemy::updateBullets(Real realTime) {	
	b.update(realTime,cam);
	//if(b.isProjectileActive())
				
	//b2.update(realTime,cam);	
}


//Gets the location for the robot
Vector3 WalkingEnemy::getLocation(){
	return mNode->getPosition();
}

//Returns the active bullets fired by the enemy
std::list<Bullet *> WalkingEnemy::activeBullets() {
	int i;
	std::list<Bullet *> active;
	Bullet* temp;
	/*for(i = 0; i < NUM_ACTIVE_BULLETS; i++) {
		if(bullets[i].isProjectileActive()) {
			temp = &bullets[i];
			active.push_back(temp);
		} 
	}*/
	if(b.isProjectileActive()) {
	active.push_back(&b);
	}

	return active;
}